package yzserver.sx4a.permission;

import com.sitech.ibnms.util.RandomGUID;
import com.sitech.portal.system.dao.ISysModuleDao;
import com.sitech.portal.system.dao.ISysRoleModuleDao;
import com.sitech.portal.system.domain.TblSYS_MODULE;
import yzserver.sx4a.RetTools;
import yzserver.sx4a.dao.IPermissionDao;
import yzserver.sx4a.domain.PermissionDomain;
import yzserver.sx4a.form.Permission;
import yzserver.sx4a.form.Ret;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: andrew
 * Date: 14-8-15
 * Time: 上午10:14
 * To change this template use File | Settings | File Templates.
 */
public class Sx4aPermissionImpl implements Sx4aPermission {

    private IPermissionDao permissionDao;
    private ISysModuleDao sysModuleDao;
    private ISysRoleModuleDao sysRoleModuleDao;

    public void setSysRoleModuleDao(ISysRoleModuleDao sysRoleModuleDao) {
        this.sysRoleModuleDao = sysRoleModuleDao;
    }

    public void setSysModuleDao(ISysModuleDao sysModuleDao) {
        this.sysModuleDao = sysModuleDao;
    }
    public void setPermissionDao(IPermissionDao permissionDao) {
        this.permissionDao = permissionDao;
    }

    @Override
    public Permission selPermission(String permissionid) {
        PermissionDomain permissionDomain  = permissionDao.selPermission(permissionid);
        if(permissionDomain!=null){
            Permission permission = new Permission(permissionDomain);
            return permission;
        }else{
            return null;
        }

    }

    @Override
    public Ret addPermission(Permission permission) {
        try {
            permissionDao.addPermission(new PermissionDomain(permission));
        } catch (Exception e) {
            return RetTools.getRet("创建权限失败", e);
        }

        //portal
        TblSYS_MODULE obj = new TblSYS_MODULE();
        obj.setMODULE_ID(permission.getPermissionid());
        if("0".equals(permission.getEnable())){
            obj.setMODULE_ENABLE(1);
        }else if("1".equals(permission.getEnable())){
            obj.setMODULE_ENABLE(0);
        }
        obj.setMODULE_NAME(permission.getPermissionname());
        obj.setMODULE_PATH(permission.getPermissionurl());
        obj.setIS_REFRESH("1");
        obj.setPARENT_ID(permission.getPermissionparent());
        obj.setMODULE_TARGET("tab");
        String directory = sysModuleDao.getModuleDirectory(obj.getPARENT_ID());
        if(directory!=null)
            directory += obj.getMODULE_NAME() + "/";
        else
            directory = obj.getMODULE_NAME() + "/";
        obj.setMODULE_DIRECTORY(directory);
        try {
            sysModuleDao.add(obj);
        } catch (Exception e) {
            return RetTools.getRet("创建权限失败", e);
        }
        return RetTools.getRet("创建权限成功", null);
    }

    @Override
    public Ret updPermission(Permission permission) {
        try {
            permissionDao.updPermission(new PermissionDomain(permission));
        } catch (Exception e) {
            return RetTools.getRet("修改权限失败", e);
        }

        //portal
        TblSYS_MODULE obj = new TblSYS_MODULE();
        obj.setMODULE_ID(permission.getPermissionid());
        if("0".equals(permission.getEnable())){
            obj.setMODULE_ENABLE(1);
        }else if("1".equals(permission.getEnable())){
            obj.setMODULE_ENABLE(0);
        }
        obj.setMODULE_NAME(permission.getPermissionname());
        obj.setMODULE_PATH(permission.getPermissionurl());
        obj.setIS_REFRESH("1");
        obj.setPARENT_ID(permission.getPermissionparent());
        obj.setMODULE_TARGET("tab");
        String directory = sysModuleDao.getModuleDirectory(obj.getPARENT_ID());
        if(directory!=null)
            directory += obj.getMODULE_NAME() + "/";
        else
            directory = obj.getMODULE_NAME() + "/";
        obj.setMODULE_DIRECTORY(directory);
        try {
            sysModuleDao.update(obj);
        } catch (Exception e) {
            return RetTools.getRet("修改权限失败", e);
        }
        return RetTools.getRet("修改权限成功", null);
    }

    @Override
    public Ret delPermission(String permissionid) {
        try {
            permissionDao.delPermission(permissionid);
        } catch (Exception e) {
            return RetTools.getRet("删除权限失败", e);
        }
        //portal
        try {
            sysModuleDao.deleteByPKs(permissionid);
            sysRoleModuleDao.deleteByModIDS(permissionid);
        } catch (Exception e) {
            return RetTools.getRet("删除权限失败", e);
        }
        return RetTools.getRet("删除权限成功", null);
    }

    @Override
    public List selAllPermission(String permissionid) {
        List<PermissionDomain> PermissionDomains = new ArrayList<PermissionDomain>();
        List  permissionList = new ArrayList();
        if("all".equalsIgnoreCase(permissionid)){ // all为接口约定的固定值
            PermissionDomains = permissionDao.selAllPermission(permissionid);
            if(PermissionDomains!=null){
                for (int i = 0; i <PermissionDomains.size(); i++) {
                    Permission permission = new Permission(PermissionDomains.get(i));
                    permissionList.add(permission);
                }
            }
        }
        return permissionList;
    }

}
